package org.hotDating.android.individual.mmi;



import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class DatingMap extends MapActivity {

	private static final int DIALOG_LOGIN = 0;
	
	View mTextEntryView;
	EditText mEtName;
	EditText mEtPassword;
    private Menu mMenu;
	
    static MapView mMapView;
    static MyLocationOverlay mMyLocationOverlay;
    static ImageButton rePosition;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		createMap();
		
		rePosition = (ImageButton) findViewById(R.id.main_button_rePosition);
		rePosition.setBackgroundResource(R.drawable.bg_button);
//		rePosition.setBackgroundDrawable(rePosition.isPressed()? getResources().getDrawable(R.drawable.dating): null);
//		rePosition.setAlpha(128);
		rePosition.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View view) {
		    	if(mMapView != null && mMyLocationOverlay != null){
		    		GeoPoint gp = mMyLocationOverlay.getMyLocation();
//				    	mMapView.getController().setCenter(gp);
		    	}
		    	rePosition.setImageResource(R.drawable.reposition);
		    }
		});
		
		rePosition.setOnTouchListener(new View.OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				rePosition.setImageResource(R.drawable.reposition_touch);
				return false;
			}
			
		});

//		ImageButton invitation = (ImageButton) findViewById(R.id.main_button_invitation);
//		invitation.setBackgroundResource(R.drawable.bg_button);
//		invitation.setOnClickListener(new View.OnClickListener() {
//		    public void onClick(View view) {
//		    	Intent i = new Intent(DatingMap.this, Invitation.class);
//		    	startActivity(i);
//		    }
//		});
		Frame.setup(this);
	}
	
	void createMap(){
		FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
        // Add the map view to the frame
        mMapView = new MapView(this, "Panoramio_DummyAPIKey");
        frame.addView(mMapView, 
                new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        // Create an overlay to show current location
        mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
        mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
            mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
        }});

        mMapView.getOverlays().add(mMyLocationOverlay);
        mMapView.getController().setZoom(15);
        mMapView.setClickable(true);
        mMapView.setEnabled(true);
        mMapView.setSatellite(true);
        
        addZoomControls(frame);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Hold on to this
        mMenu = menu;
        
        // Inflate the currently selected menu XML resource.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        
        return true;
    }


	
/*
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_LOGIN:
			LayoutInflater factory = LayoutInflater.from(this);
			mTextEntryView = factory.inflate(
					R.layout.alert_dialog_login, null);
			mEtName = (EditText) findViewById(R.id.username_edit);
//			mEtName.setText("@gmail.com");
			mEtPassword = (EditText) findViewById(R.id.password_edit);
			return new AlertDialog.Builder(hotDating.this).setIcon(
					R.drawable.alert_dialog_icon).setTitle(
					R.string.alert_dialog_login_title).setView(mTextEntryView)
					.setPositiveButton(R.string.alert_dialog_login_ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									if (isValidate()) {
//										dismissDialog(DIALOG_LOGIN);
									}
								}
							}).setNegativeButton(
							R.string.alert_dialog_login_cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

									
								}
							}).create();
		default:
			throw new IllegalArgumentException();
		}
	}*/

	
    /**
     * Add zoom controls to our frame layout
     */
    private void addZoomControls(FrameLayout frame) {
        // Get the zoom controls and add them to the bottom of the map
        View zoomControls = mMapView.getZoomControls();

        FrameLayout.LayoutParams p = 
            new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, Gravity.BOTTOM + Gravity.CENTER_HORIZONTAL);
        frame.addView(zoomControls, p);
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
//	private class LoginButtonListener implements OnClickListener {
//		public void onClick(View v) {
//			if (onLoginListener != null) {
//				isLoginCanceled = false;
//				if (timeout != 0)
//					handler.sendEmptyMessageDelayed(LOGIN_TIMEOUT, timeout);
//				loginThread = new LoginThread();
//				loginThread.start();
//			}
//		}
//	}
}